/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thynk
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class AddProductPanel extends JPanel{

    JLabel nameLabel, quantityLabel, rawMaterialName, numRMLabel;
    JTextField nameField, quantityField, numRMField;
    JList rawMaterialList;
    JTextArea copyList;
    JButton getButton;
    JScrollPane js1, js2;
    private ArrayList inventoryIndex;
    private ArrayList invNumNeeded;

    public AddProductPanel() {

        // set Layout
        nameLabel = new JLabel("Name");
        nameField = new JTextField();
        quantityLabel = new JLabel("Quantity");
        quantityField = new JTextField();
        rawMaterialName = new JLabel("Raw Material");
        final String [] arrayOfProducts = (String []) getInventoryName().toArray
                                          (new String[ getInventoryName().size()]);
        rawMaterialList = new JList( arrayOfProducts );
        rawMaterialList.setSelectionMode (
                ListSelectionModel.SINGLE_INTERVAL_SELECTION );

        numRMLabel = new JLabel("Number");
        numRMField = new JTextField();

        copyList = new JTextArea();
        copyList.setEditable(false);

        getButton = new JButton( "GET >>>" );

        // copy text selected to text area
        getButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
                //System.out.println(rawMaterialList.getSelectedValues().toString());
                int index = rawMaterialList.getSelectedIndex();
                int num = Integer.parseInt(numRMField.getText());
                copyList.append(arrayOfProducts[index] + ":" + num + '\n');
//                invNumNeeded.add(num);
                numRMField.setText("");
            }
        });

        js1 = new JScrollPane(rawMaterialList);
        js2 = new JScrollPane(copyList);

        add(nameLabel);
        add(nameField);
        add(quantityLabel);
        add(quantityField);
        add(rawMaterialName);
        add(js1);
        //add(rawMaterialList);
        add(getButton);
        add(js2);
        add(numRMLabel);
        add(numRMField);
    }

    public Dimension getPreferredSize() {
        return new Dimension(350, 200);
    }

    public Dimension getMinimumSize() {
        return new Dimension(350, 200);
    }

    public void doLayout() {
        nameLabel.setBounds(10, 10, 60, 30);
        nameField.setBounds(70, 15, 270, 20);
        quantityLabel.setBounds(10, 40, 60, 30);
        quantityField.setBounds(70, 45, 270, 20);
        rawMaterialName.setBounds(10, 70, 100, 30);
        js1.setBounds(10, 100, 150, 100);
        //rawMaterialList.setBounds(10, 100, 150, 100);
        getButton.setBounds(180, 150, 100, 30);
        js2.setBounds(300, 100, 150, 100);
        numRMLabel.setBounds(10, 200, 270, 30);
        numRMField.setBounds(300, 200, 60, 30);

    }

    /* get list of inv to show on rawMaterialList */
    private ArrayList<String> getInventoryName() {
        ArrayList<String> temp = new ArrayList<String>();

        for (int i = 0; i < SupplyChainManagement.inv.size(); ++i) {
            temp.add(SupplyChainManagement.inv.get(i).getNameProduct());
        }

        return temp;
   }

   public void reset() {
       nameField.setText("");
       quantityField.setText("");
       numRMField.setText("");
       copyList.setText("");

   }

   /* return inventory name from text area */
   public ArrayList getInventory() {
       //ListModel model = copyList.getModel();
       inventoryIndex = new ArrayList();
       invNumNeeded = new  ArrayList();
       String text = copyList.getText();
       StringTokenizer st = new StringTokenizer(text, "\n");

       for (int i = 0; i < copyList.getLineCount()-1; ++i) {
           //System.out.println(copyList.getLineCount());
           String line = st.nextToken();
           StringTokenizer temp = new StringTokenizer(line, ":");

           inventoryIndex.add(temp.nextToken()); // set name
           //System.out.print(inventoryIndex.get(i));        
           invNumNeeded.add(temp.nextToken()); // set amount needed
       }

       return inventoryIndex;
   }

   /* return num inventory needed for each froduct */
   public ArrayList getNum() {
       return invNumNeeded;
   }
}
